﻿namespace Raven
{
    public class Blaster : RavenWeapon
    {
        protected override void InitializeFuzzyModule()
        {
            _fuzzyModule = new FuzzyModule();
            FuzzyVariable DistToTarget = _fuzzyModule.CreateFLV("DistToTarget");

            FzSet Target_Close = DistToTarget.AddLeftShoulderSet("Target_Close", 0, 25, 150);
            FzSet Target_Medium = DistToTarget.AddTriangularSet("Target_Medium", 25, 150, 300);
            FzSet Target_Far = DistToTarget.AddRightShoulderSet("Target_Far", 150, 300, 1000);

            FuzzyVariable Desirability = _fuzzyModule.CreateFLV("Desirability");
            FzSet VeryDesirable = Desirability.AddRightShoulderSet("VeryDesirable", 50, 75, 100);
            FzSet Desirable = Desirability.AddTriangularSet("Desirable", 25, 50, 75);
            FzSet Undesirable = Desirability.AddLeftShoulderSet("Undesirable", 0, 25, 50);

            _fuzzyModule.AddRule(Target_Close, Desirable);
            _fuzzyModule.AddRule(Target_Medium, new FzVery(Undesirable));
            _fuzzyModule.AddRule(Target_Far, new FzVery(Undesirable));
        }

        public override void Destroy()
        {
        }

        public override void ShootAt(Vector2 pos)
        {
            if (isReadyForNextShot())
            {
                //fire!
                _owner.GetWorld().AddBolt(_owner, pos);

                UpdateTimeWeaponIsNextAvailable();

                //add a trigger to the game so that the other bots can hear this shot
                //(provided they are within range)
                _owner.GetWorld().GetMap().AddSoundTrigger(_owner, Constants.Blaster_SoundRange);
            }
        }

        public override Fix GetDesirability(Fix DistToTarget)
        {
            //fuzzify distance and amount of ammo
            _fuzzyModule.Fuzzify("DistToTarget", DistToTarget);

            _lastDesirabilityScore = _fuzzyModule.DeFuzzify("Desirability");

            return _lastDesirabilityScore;
        }

        public Blaster(RavenBot owner) : base(WeaponType.type_blaster, Constants.Blaster_DefaultRounds,
            Constants.Blaster_MaxRoundsCarried,
            Constants.Blaster_FiringFreq, Constants.Blaster_IdealRange, Constants.Bolt_MaxSpeed, owner)
        {
            //setup the fuzzy module
            InitializeFuzzyModule();
        }
    }
}